package com.gwtplatform.plugin.projectfile.war;

import java.io.ByteArrayInputStream;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;

import com.gwtplatform.plugin.projectfile.ProjectFile;

public class ApplicationContextXML extends ProjectFile {

	public ApplicationContextXML(IProject project, IPath path)
			throws CoreException {
		super(project, path, "applicationContext.xml");
	}

	public IFile createFile(IFile projectHTML, String serverModule) throws CoreException {

		String contents = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
		contents += "<beans xmlns=\"http://www.springframework.org/schema/beans\"\n";
		contents += "\t\txmlns:p=\"http://www.springframework.org/schema/p\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
		contents += "\t\txmlns:aop=\"http://www.springframework.org/schema/aop\" xmlns:context=\"http://www.springframework.org/schema/context\"\n";
		contents += "\t\txmlns:tx=\"http://www.springframework.org/schema/tx\" xmlns:task=\"http://www.springframework.org/schema/task\"\n";
		contents += "\t\t\txsi:schemaLocation=\"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd\n";
		contents += "\t\t\thttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd\n";
		contents += "\t\t\thttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd\n";
		contents += "\t\t\thttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd\n";
		contents += "\t\t\thttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd\">\n\n";

		contents += "\t\t<context:component-scan base-package=\"com.gwtplatform.dispatch.server.spring\" />\n\n";		

		contents += "\t\t<bean class=\""+serverModule+"\" />\n\n";
		contents += "\t\t<bean id=\"logger\" class=\"com.gwtplatform.dispatch.server.spring.LoggerFactoryBean\">\n";
		contents += "\t\t\t<constructor-arg>\n";
		contents += "\t\t\t\t<bean class=\"java.util.logging.Logger\" factory-method=\"getAnonymousLogger\" />\n";
		contents += "\t\t\t</constructor-arg>\n";
		contents += "\t\t</bean>\n";
		contents += "</beans>";		

		file.create(new ByteArrayInputStream(contents.getBytes()), false, null);

		return file;
	}

}
